<?php
namespace App\Services;

use Illuminate\Http\Client\RequestException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Illuminate\Support\Facades\Http;

class AdminService {
    /**
     * @notes 发起公共请求
     * @param $path
     * @param $data
     * @param $header
     * @return mixed
     * @throws RequestException
     * @author Evil丿
     * @date 2024/06/06 15:33
     */
    private static function commonRequest($path, $data, $method = "GET", $header = [])
    {
        if(strpos($path, 'http') === false) {
            $host = config('app.admin_url');
            $url = $host . $path;
        } else {
            $url = $path;
        }

        if($method == "POST") {
            $response = Http::retry(3, 1000)
                -> timeout(3)
                -> withHeaders($header)
                -> post($url, $data)
                -> throw()
                -> json();

        } else {
            $response = Http::retry(3, 1000)
                -> timeout(3)
                -> withHeaders($header)
                -> get($url, $data)
                -> throw()
                -> json();
        }

        if(!$response) {
            throw new AccessDeniedHttpException('商城服务请求失败');
        }

        if($response['code'] != 1) {
            throw new AccessDeniedHttpException($response['msg']);
        }

        return $response;
    }


    /**
     * @notes 同步用户收益
     * @param $user_id
     * @param $amount
     * @param $order_id
     * @param $order_sn
     * @param $machine_no
     * @param $type
     * @return mixed
     * @throws RequestException
     * @author Evil丿
     * @date 2024/07/29 12:52
     */
    public static function setUserProfit($user_id, $amount, $order_id, $order_sn, $machine_no, $type)
    {
        $params = [
            "user_id"    => $user_id,
            "amount"     => $amount,
            "order_id"   => $order_id,
            "order_sn"   => $order_sn,
            "machine_no" => $machine_no,
            "type"       => $type
        ];

        $path = '/api/v1.device/setUserProfit';

        $res = self::commonRequest($path, $params, "POST");

        return $res;
    }

    public static function getUserList($page_no = 1, $page_size = 10, $user_sn = "", $nickname = "")
    {
        $params = [
            "page_no"   => $page_no,
            "page_size" => $page_size,
            "user_sn"   => $user_sn,
            "nickname"  => $nickname,
        ];

        $path = '/api/v1.device/getUserList';

        $res = self::commonRequest($path, $params);

        return $res;
    }

    public static function getUserInfo($id) {

        $params = [
            "user_id" => $id
        ];

        $path = '/api/v1.device/getUserInfo';

        $res = self::commonRequest($path, $params);

        return $res;
    }

    public static function setDeviceCompensation($params)
    {
        $path = '/api/v1.device/setDeviceCompensation';

        $res = self::commonRequest($path, $params,'POST');

        return $res;
    }

    public static function setDeviceDeposit($params)
    {
        $path = '/api/v1.device/setDeviceDeposit';

        $res = self::commonRequest($path, $params,'POST');

        return $res;
    }

    public static function getCardUserInfo($params)
    {
        $path = '/api/user/getUserCardToMachine';

        $params['access_token'] = '123456';

        try{
            $res = self::commonRequest($path, $params,'POST');
        }catch (AccessDeniedHttpException $e){
            return [];
        }

        return $res['data'];
    }
}
